//
//  NSOperationQueueAdditions.m
//  Tint
//
//  Created by Kevin Wu on 2/22/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "NSOperationQueueAdditions.h"

@implementation NSOperationQueue (Tint)


#pragma mark - Singleton

static NSOperationQueue *sharedOperationQueue = nil;

+ (NSOperationQueue *)sharedObject
{
	if ( sharedOperationQueue == nil ) {
        sharedOperationQueue = [[self alloc] init];
        [sharedOperationQueue setMaxConcurrentOperationCount:4];
    }
    return sharedOperationQueue;
}

+ (BOOL)hasSharedObject
{
    return ( sharedOperationQueue != nil );
}

+ (void)destroySharedObject
{
    if ( sharedOperationQueue ) {
        [sharedOperationQueue cancelAllOperations];
        [sharedOperationQueue release];
        sharedOperationQueue = nil;
    }
}



#pragma mark - Safely operation

- (void)addOperationSafely:(NSOperation *)operation
{
    if (([operation isCancelled]) || 
        ([operation isExecuting]) || 
        ([operation isFinished]) || 
        ([[self operations] indexOfObjectIdenticalTo:operation] != NSNotFound))
    {
        return;
    }
    
    [self addOperation:operation];
}

@end
